package com.jiuling.qiniu.oss;

import com.google.gson.Gson;
import com.jiuling.qiniu.oss.config.QiNiuOssConfig;
import com.jiuling.qiniu.oss.model.FileUploudModel;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;

import java.io.InputStream;

/**
 * 文件上传
 * @author 紫灵
 * @date 2023年05月23日 13时57分16秒
 */
public class FileUploud {

    private final QiNiuOssConfig qiNiuOssConfig;
    private final Configuration configuration;

    public  FileUploud(QiNiuOssConfig qiNiuOssConfig) {
        this.qiNiuOssConfig = qiNiuOssConfig;
        this.configuration =  new Configuration(qiNiuOssConfig.getRegion());
    }

    public FileUploudModel fileUploud(String fileKey,String filePath){
        configuration.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;// 指定分片上传版本
        UploadManager uploadManager = new UploadManager(configuration);
        String upToken = getAuth().uploadToken(qiNiuOssConfig.getBucket());
        FileUploudModel fileUploudModel = null;
        try {
            Response response = uploadManager.put(filePath, fileKey, upToken);
            //解析上传成功的结果
            fileUploudModel = defaultPutRetToFileUploudModel(new Gson().fromJson(response.bodyString(), DefaultPutRet.class));
        } catch (QiniuException e) {
            Response r = e.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ignore) {
            }
        }
        return fileUploudModel;
    }

    public FileUploudModel dataStreamUploud(String fileKey,InputStream inputStream,boolean cover){
        configuration.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;// 指定分片上传版本
        UploadManager uploadManager = new UploadManager(configuration);
        String upToken;
        if(cover){
            upToken = getAuth().uploadToken(qiNiuOssConfig.getBucket(),fileKey);
        }else {
            upToken = getAuth().uploadToken(qiNiuOssConfig.getBucket());
        }
        FileUploudModel fileUploudModel = null;
        try {
            Response response = uploadManager.put(inputStream,fileKey,upToken,null, null);
            //解析上传成功的结果
            fileUploudModel = defaultPutRetToFileUploudModel(new Gson().fromJson(response.bodyString(), DefaultPutRet.class));
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ignore) {
            }
        }
        return fileUploudModel;
    }

    private Auth getAuth(){
        return Auth.create(qiNiuOssConfig.getQiNiuConfig().getAccessKey(), qiNiuOssConfig.getQiNiuConfig().getSecretKey());
    }

    /**
     * 默认返回转文件上传模型
     * @param defaultPutRet 默认返回
     * @return 文件上传模型
     */
    public FileUploudModel defaultPutRetToFileUploudModel(DefaultPutRet defaultPutRet){
        FileUploudModel fileUploudModel = new FileUploudModel();
        fileUploudModel.setHash(defaultPutRet.hash);
        fileUploudModel.setKey(defaultPutRet.key);
        return fileUploudModel;
    }

}
